package Test_Web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet
 */
public class LoginPage extends HttpServlet {
	private static final long serialVersionUID = 1L;
	String firstName = "";
	String lastName = "";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginPage() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String email = "";
		String password = "";
		email = request.getParameter("Email");
		password = request.getParameter("Password");
		
		// build sql command
		try
		{
			try {
				Class.forName("com.mysql.jdbc.Driver").newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
			Connection connect;
			connect = DriverManager.getConnection("jdbc:mysql:///moviedb","testuser","testpass");
			Statement tempStatement = connect.createStatement();
			String command = "select * from customers where email = '" +
			"" +email+"' AND password ='" + password + "'";
		
			// execute query
			ResultSet resultSet = tempStatement.executeQuery(command);

		
			// if it has >= 1 result, go to main page
			if(!resultSet.isBeforeFirst()) //if invalid password/email
			{
				//redirect back here
				response.sendRedirect("LoginPage.jsp");
			}
			// if it is not valid
			else
			{
				//redirect to main page
				response.sendRedirect("MainPage.jsp");
			}
			
			resultSet.close();
            tempStatement.close();
            connect.close();
		}
		catch (SQLException e) {
			e.printStackTrace();
		} 
	}
}
